<?php

namespace app\admin\controller;
use think\Db;
use app\common\controller\order\Orderget;
use app\common\controller\chuanglan\ChuanglanSmsApi as CLApi;
use app\common\controller\system\Docking;
use app\admin\controller\system\SmsTemplate;
class Orderhuankuan extends Common
{
    //	后台代扣 手动操作  战棋 还款
    public function daokou_caozuo()
    {
        $Orderget = new Orderget();
        $datainput = input();
        $data['type'] = input('type'); //还款类型 1还款 2续期
        $data['CreateTime'] = date('Y-m-d H:i:s');
        $order = Db::table('xb_loans_applylist')->where('ID', $datainput['ID'])->find(); //订单数据
       
        if (empty($order)) {
            show_error('', '无效的订单ID');
        } else {
            $data['OrderSn'] = $order['OrderSn']; //applylist 订单号
                $Payceshi = $Orderget->get_Payceshi(); //获取支付环境
                if ($Payceshi == '1') {
                    if ($data['type'] == '1') {
                        $data['Money'] = $order['daoqiMoney']; //还款   应还金额 到期
                    } elseif ($data['type'] == '2') {
                        $data['Money'] = $order['zhanqifei']; //续期   应还金额 到期
                    } elseif ($data['type'] == '3') {
                    	
                        $data['Money'] = $datainput['daoqiMoney']; //还款   应还金额  逾期
                    } elseif ($data['type'] == '4') {
                        $data['Money'] = $order['zhanqifei']; //续期   应还金额 逾期
                    } else {
                        $data['Msg'] = '支付环境错误';
                        Db::table('xb_daikou')->insert($data);
                    }
                } else {
                    $data['Money'] = '0.01'; //放款金额
                }
        }
        $user = Db::table('xb_mem_info')->where('ID', $order['UserID'])->find(); //用户数据
           $ret = Db::table('xb_renzen_bank')->where('UserID', $order['UserID'])->find(); //银行卡数据
           if ($ret) {
               $data['BankNo'] = $ret['BankNo']; //银行卡号
           } else {
               $data['Msg'] = '用户银行卡获取失败';
               Db::table('xb_daikou')->insert($data);
           }
        $changpay = new \changpay\ChangPay();
        $data['PartnerId'] = $Orderget->get_changpay(); //获取商户号
           $data['userId'] = $order['UserID']; //用户标志
           $data['is_zidong'] = '2'; //1定时任务自动扣 2 手动扣款一个一个扣
//         
//         p($data);die;
           $datas = $changpay->changjie_bank($data);
        //		   p($datas);die;
        if ($datas['AcceptStatus'] == 'S') {
            $data['Msg'] = '提交成功,等待扣款';
            Db::table('xb_daikou')->insert($data);
            show_success('0', $message = '提交成功,等待扣款');
        } else {
            $data['Msg'] = $datas['RetMsg'];
            Db::table('xb_daikou')->insert($data);
            show_error('', $datas['RetMsg']);
        }
    }

    //催收备注添加页面
    public function beizhu()
    {
        $ID = input('ID');
        $this->assign('ID', $ID);

        return $this->fetch('orderyuqi/beizhu');
    }

    //	设置催收人员页面
    public function couishou_people_add()
    {
        $data = Db::table('xb_sys_administrator')->where(['RoleID' => '51'])->select(); //用户ID
        $this->assign('data', $data);

        return $this->fetch('orderyuqi/couishou_people_add');
    }

    //	豁免页面
    public function huomian()
    {
        $ID = input(); //订单ID
        $data = Db::table('xb_loans_applylist')->where('ID', $ID['ID'])->find(); //订单全部数据
        $Orderget = new Orderget();
        $data['dingdan_yinghuan_money'] = $Orderget->get_dingdan_yinghuan_money($data); //订单应还
        $data['muqian_yinghuan_money'] = $Orderget->get_muqian_yinghuan_money($data); // 目前应还
        $data['yuqimoney'] = $Orderget->get_yinghuan_yuqi($data); //获取剩余应还逾期金额
        $this->assign('data', $data);

        return $this->fetch();
    }

    //	详情里面的订单里面的 还款记录 数据
    public function huankuan_list_ss()
    {
        $ID = input('ID'); //订单ID
        $page = input('page');
        $limit = input('limit');
        $xloans_xjapplylist = Db::table('xb_loans_hklist')->where('ApplyID', $ID)->select();
        $Orderget = new Orderget();
        $xloans_xjapplylist = $Orderget->get_Type_pay($xloans_xjapplylist);
        //数据分页
        $cuont = count($xloans_xjapplylist);
        $star = ($page - 1) * $limit; //开始截取位置
        $re = array_slice($xloans_xjapplylist, $star, $limit); //分页处理
        if ($xloans_xjapplylist) {
            songsong($xloans_xjapplylist, $cuont);
        } else {
            songsong($xloans_xjapplylist, $cuont);
        }
    }

    //	详情里面的订单里面的 展期记录 数据
    public function zhanqi_list_ss()
    {
        $ID = input('ID'); //订单ID
        $page = input('page');
        $limit = input('limit');
        $xloans_xjapplylist = Db::table('xb_loans_xjapplylist')->where('AppID', $ID)->select(); //
        //		p($xloans_xjapplylist);
        $Orderget = new Orderget();
        $xloans_xjapplylist = $Orderget->get_Type_pay($xloans_xjapplylist); //获取划款类型 还款方式
//      数据分页
        $cuont = count($xloans_xjapplylist);
        $star = ($page - 1) * $limit; //开始截取位置
        $re = array_slice($xloans_xjapplylist, $star, $limit); //分页处理
        if ($xloans_xjapplylist) {
            songsong($xloans_xjapplylist, $cuont);
        } else {
            songsong($xloans_xjapplylist, $cuont);
        }
    }

    //	后台代扣页面   展期代扣  还款代扣
    public function daikou()
    {
        $ID = input(); //订单ID
        $data = Db::table('xb_loans_applylist')->where('ID', $ID['ID'])->find();
        $this->assign('data', $data);

        return $this->fetch();
    }

    //	后台展期页面面
    public function zhanqi()
    {
        $ID = input(); //订单ID
        $data = Db::table('xb_loans_applylist')->where('ID', $ID['ID'])->find();
        $this->assign('data', $data);

        return $this->fetch();
    }

    //	后台展期操作
    public function zhanqi_caozuo()
    {
        $data = input(); //所有 数据
        //		p($data);die;
        $loans_applylist_data = Db::table('xb_loans_applylist')->where('ID', $data['ID'])->find();
        $UserID = $loans_applylist_data['UserID'];
        if ($loans_applylist_data['LoanStatus'] == '3') {
            $code = '404';
            $message = '非法操作,订单已经完成';
            show_error($code, $message);
        }
        Db::startTrans();
        try {
            $Orderget = new Orderget();
            $get_days_fakuan = $Orderget->get_days_fakuan(strtotime(date('Y-m-d'), time()), strtotime($loans_applylist_data['YyFkTime']), $loans_applylist_data);

            if ($get_days_fakuan['datas'] == '0') {
                $app_type = '3';
            } else {
                $app_type = '4';
            }
            $ApplyDay = $loans_applylist_data['ApplyDay'];
            $ApplyDay = $ApplyDay - 1;
            $YyFkTime = $Orderget->get_time($loans_applylist_data['YyFkTime'], $ApplyDay);
            $loans_applylist = [
        'YyFkTime' => $YyFkTime, //预约还款时间
        'is_fujie' => '1', //是否展期
        'xjnums' => $loans_applylist_data['xjnums'] + 1, //展期次数
        'app_type' => $app_type, //订单类型
        ];
            $loans_applylist_re = Db::table('xb_loans_applylist')->where('ID', $data['ID'])->update($loans_applylist);
            //		写入展期表  订单号为 唯一连接条件
            $loans_xjapplylist = [
          'OrderSn' => $data['OrderSn'], //订单号
          'TotalMoney' => $data['zhanqifei'], //展期飞
          'LoanDay' => $data['zhanqi_num'], //展期天数
          'Type' => 7, //还款类型
          'pay' => $data['pay'], //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
          'PayTime' => date('Y-m-d H:i:s'), //展期时间  支付时间
          'CreateTime' => date('Y-m-d H:i:s'), //订单生成时间
          'UserID' => $UserID, //用户ID
          'AppID' => $data['ID'], //订单ID
          'YyFkTime' => $YyFkTime, //预约还款日
           'TradeNo' => time(), //交易流水
        ];
            $loans_xjapplylist_re = Db::table('xb_loans_xjapplylist')->insertGetId($loans_xjapplylist);

//      写入支付记录表
            $loans_paylist = [
            'OrderSn' => $data['OrderSn'], //订单号
            'CreateTime' => date('Y-m-d H:i:s'), //订单生成时间//支付时间
            'pay' => $data['pay'], //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
            'Type' => 7, //还款类型 1部分还款2结清3豁免 4线上还款 5线上展期 6线下还款7线下展期
            'UserID' => $UserID, //用户ID
            'AppID' => $data['ID'], //订单ID
            'OrderAmount' => $data['zhanqifei'], //展期飞  支付金额
            'TradeNo' => time(), //交易流水
            'caozuorenID' => session('id'), //操作人ID
        ];
            $loans_paylist_re = Db::table('xb_loans_paylist')->insertGetId($loans_paylist);
            Db::commit();
            //		发送短信操作
            $smsTemp = (new SmsTemplate())->getSmsInfo('zhanqi');
            $userInfo = db('loans_applylist')->alias('a')
                      ->where('a.ID', $data['ID'])
                      ->join('xb_mem_info m', 'a.UserID = m.ID')
                      ->find();
            $bankInfo = db('renzen_bank')->where(['UserID' => $UserID])->find();
            $smsTempStr = str_replace(['#name#', '#time#'], [$userInfo['TrueName'], $userInfo['YyFkTime']], $smsTemp['Content']);
//              //发送短信
            $result1 = (new CLApi())->sendSMS($userInfo['Mobile'], $smsTempStr);
            $result2 = json_decode($result1, true);
            if ($result2['code'] != 0) {
                \Think\Log::record('放款操作 发送短信失败 数据 - '.$result1);
            }
            //写入对接产品记录表
            $re = (new Docking())->addDocking(1, ($result2['code'] == 0) ? 2 : 3, 'chuanglan', '展期操作发送验证码(后台展期)', $result1);
            show_success(); //返回数据
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            //			p($e->getMessage());die;
            show_error();
        }
    }

    //	后台还款页面
    public function huankuan()
    {
        $ID = input(); //订单ID
        $data = Db::table('xb_loans_applylist')->where('ID', $ID['ID'])->find(); //订单全部数据
        $Orderget = new Orderget();
        $data['dingdan_yinghuan_money'] = $Orderget->get_dingdan_yinghuan_money($data); //订单应还
        $data['muqian_yinghuan_money'] = $Orderget->get_muqian_yinghuan_money($data); // 目前应还
        $data['yuqimoney'] = $Orderget->get_yinghuan_yuqi($data); //获取剩余应还逾期金额
        $this->assign('data', $data);

        return $this->fetch();
    }
	  //	后台逾期列表里面的代扣2页面
    public function daikou2()
    {
        $ID = input(); //订单ID
        $data = Db::table('xb_loans_applylist')->where('ID', $ID['ID'])->find(); //订单全部数据
        $Orderget = new Orderget();
        $data['dingdan_yinghuan_money'] = $Orderget->get_dingdan_yinghuan_money($data); //订单应还
        $data['muqian_yinghuan_money'] = $Orderget->get_muqian_yinghuan_money($data); // 目前应还
        $data['yuqimoney'] = $Orderget->get_yinghuan_yuqi($data); //获取剩余应还逾期金额
        $this->assign('data', $data);

        return $this->fetch();
    }
	  //	后台逾期列表里面的代扣2页面
    public function daikou2_caozuo()
    {
        $data = input(); //订单ID
        p($data);die;
//		明天接着写
      
    }

    //	后台还款操作
    public function huankuan_caozuo()
    {
        $data = input(); //订单ID
        //		p($data);die;
        //		判断当前订单是否可以还款
        $loans_applylist_re = Db::table('xb_loans_applylist')->where('ID', $data['ID'])->find();
        if ($loans_applylist_re['LoanStatus'] == '3') {
            $code = '404';
            $message = '非法操作,订单已经完成';
            show_error($code, $message);
        }
        if ($data['Type'] == '3') {//豁免
            // 启动事务
            Db::startTrans();
            try {
                //		写入还款记录表
                $moneyshiji = $data['benjin_homian_Money'] + $data['faxi_homian_Money'];
                $loans_hklist = [
            'OrderSn' => $data['OrderSn'], //订单号
            'TotalMoney' => $moneyshiji, //实际还款金额
            'ApplyID' => $data['ID'], //订单ID
            'HkTime' => date('Y-m-d H:i:s'), //订单生成时间//支付时间
            'pay' => $data['pay'], //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
            'Type' => $data['Type'], //还款类型 1部分还款2结清3豁免 4线上还款 5线上展期 6线下还款7线下展期
            'UserID' => $loans_applylist_re['UserID'], //用户ID
            'moneysheng' => $data['moneysheng'], //剩余应还  本金+逾期费
            'TradeNo' => time(), //交易流水
        ];
                $loans_hklist_re = Db::table('xb_loans_hklist')->insertGetId($loans_hklist);
//      写入支付记录表
                $loans_paylist = [
            'OrderSn' => $data['OrderSn'], //订单号
            'CreateTime' => date('Y-m-d H:i:s'), //订单生成时间//支付时间
            'pay' => $data['pay'], //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
            'Type' => $data['Type'], //还款类型 1部分还款2结清3豁免 4线上还款 5线上展期 6线下还款7线下展期
            'UserID' => $loans_applylist_re['UserID'], //用户ID
            'AppID' => $loans_applylist_re['ID'], //订单ID
            'OrderAmount' => $moneyshiji, //实际支付金额
            'moneysheng' => $data['moneysheng'], //剩余应还  本金+逾期费
            'TradeNo' => time(), //交易流水
            'caozuorenID' => session('id'), //操作人ID
        ];
                $loans_paylist_re = Db::table('xb_loans_paylist')->insertGetId($loans_paylist);
                $this->logs('管理员豁免');
                Db::commit();
                show_success();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                //			p($e->getMessage());die;
                show_error();
            }
        }
        if ($data['Type'] == '1') {//部分还款
            //		写入还款记录表
            Db::startTrans();
            try {
                $loans_hklist = [
          'OrderSn' => $data['OrderSn'], //订单号
          'ApplyID' => $data['ID'], //订单ID
          'HkTime' => date('Y-m-d H:i:s'), //订单生成时间//支付时间
          'pay' => $data['pay'], //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
          'Type' => $data['Type'], //还款类型 1部分还款2结清3豁免 4线上还款 5线上展期 6线下还款7线下展期
          'UserID' => $loans_applylist_re['UserID'], //用户ID
          'TotalMoney' => $data['moneyshiji'], //实际支付金额
          'moneysheng' => $data['moneysheng'], // 目前应还-本次还款金额
          'TradeNo' => time(), //交易流水
        ];
                $loans_hklist_re = Db::table('xb_loans_hklist')->insertGetId($loans_hklist);
//      写入支付记录表
                $loans_paylist = [
        'OrderSn' => $data['OrderSn'], //订单号
        'AppID' => $loans_applylist_re['ID'], //订单ID
        'CreateTime' => date('Y-m-d H:i:s'), //订单生成时间//支付时间
        'pay' => $data['pay'], //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
        'Type' => $data['Type'], //还款类型 1部分还款2结清3豁免 4线上还款 5线上展期 6线下还款7线下展期
        'UserID' => $loans_applylist_re['UserID'], //用户ID
        'OrderAmount' => $data['moneyshiji'], //实际支付金额
        'moneysheng' => $data['moneysheng'], //目前应还-本次还款金额
        'TradeNo' => time(), //交易流水
        'caozuorenID' => session('id'), //操作人ID
        ];
                $loans_paylist_re = Db::table('xb_loans_paylist')->insertGetId($loans_paylist);
                //	            更改订单表为部分还款字段
                $loans_applylist['bufenhuankuan'] = '1';
                $loans_applylist['bufenhuankuan_num'] = $loans_applylist_re['bufenhuankuan_num'] + 1;
                $loans_applylist_re = Db::table('xb_loans_applylist')->where('ID', $data['ID'])->update($loans_applylist);
                //		返回结果
                $this->logs('管理员部分还款');
                Db::commit();
                show_success();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                show_error();
            }
        }
        if ($data['Type'] == '2') {//一次性结清
            //		更改订单表   状态 LoanStatus
            // 启动事务
            Db::startTrans();
            try {
                $loans_applylist = [
            'LoanStatus' => '3', //订单状态 3代表已经完成
            'SjFkTime' => date('Y-m-d H:i:s'), //实际还款时间
            'payCome' => $data['pay'], //还款方式
            ];
                //			p($data['ID']);
                //			p($loans_applylist);die;
                $loans_applylist_update = Db::table('xb_loans_applylist')->where('ID', $data['ID'])->update($loans_applylist);
                //			p($loans_applylist_update);die;

                //      写入支付记录表
                $UserID = Db::table('xb_loans_applylist')->where('ID', $data['ID'])->value('UserID');
                $loans_paylist = [
                'OrderSn' => $data['OrderSn'], //订单号
                'CreateTime' => date('Y-m-d H:i:s'), //订单生成时间//支付时间
                'pay' => $data['pay'], //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
                'Type' => $data['Type'], //还款类型 1部分还款2结清3豁免 4线上还款 5线上展期 6线下还款7线下展期
                'UserID' => $loans_applylist_re['UserID'], //用户ID
                'AppID' => $loans_applylist_re['ID'], //订单ID
                'OrderAmount' => $data['moneyshiji'], //实际支付金额
                'moneysheng' => $data['moneysheng'], //剩余应还  本金+逾期费
                'TradeNo' => time(), //交易流水
                'caozuorenID' => session('id'), //操作人ID
                ];
                $loans_paylist_re = Db::table('xb_loans_paylist')->insertGetId($loans_paylist);
                //			写入还款记录表
                $loans_hklist = [
              'OrderSn' => $data['OrderSn'], //订单号
              'TotalMoney' => $data['moneyshiji'], //实际还款金额
              'ApplyID' => $data['ID'], //订单ID
              'HkTime' => date('Y-m-d H:i:s'), //订单生成时间//支付时间
              'pay' => $data['pay'], //还款方式 1线下微信 2线下支付宝 3线下银联 4线上微信 5线上支付宝 6线上银联7豁免
              'Type' => $data['Type'], //还款类型 1部分还款2结清3豁免 4线上还款 5线上展期 6线下还款7线下展期
              'UserID' => $UserID, //用户ID
              'moneysheng' => $data['moneysheng'], //剩余应还  本金+逾期费
              'TradeNo' => time(), //交易流水
            ];
                $loans_hklist_re = Db::table('xb_loans_hklist')->insertGetId($loans_hklist);
                //		返回结果
                Db::commit();
                $this->logs('管理员结清');
                //		发送短信
                $smsTemp = (new SmsTemplate())->getSmsInfo('repayment_Inform');
                $userInfo = db('loans_applylist')->alias('a')
                      ->where('a.ID', $loans_applylist_re['ID'])
                      ->join('xb_mem_info m', 'a.UserID = m.ID')
                      ->find();
                $bankInfo = db('renzen_bank')->where(['UserID' => $loans_applylist_re['UserID']])->find();
                $smsTempStr = str_replace(['#name#', '#money#'], [$userInfo['TrueName'], $data['moneyshiji']], $smsTemp['Content']);
                $result1 = (new CLApi())->sendSMS($userInfo['Mobile'], $smsTempStr);
                $result2 = json_decode($result1, true);
                //写入对接产品记录表
                (new Docking())->addDocking(1, ($result2['code'] == 0) ? 2 : 3, 'chuanglan', '后太结清', $result1);
                show_success();
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                show_error();
            }
        }
    }

//    用户列表页面
    public function index()
    {
        $qudao = $this->qudao(); //调用渠道数据
        $this->assign('qudao', $qudao);
        $loanStatus = ['已放款', '已完成'];
        $this->assign('loanStatus', $loanStatus);

        return $this->fetch();
    }

    //   当日到期页面
    public function dangri_index()
    {
        $qudao = $this->qudao(); //调用渠道数据
        $this->assign('qudao', $qudao);
        //		订单状态  1等待放款 2已放款 3已完成 4已取消 5已拒绝 6等待银行打款 7待人工审核
        $loanStatus = [
        '2' => '已放款',
        '3' => '已完成',
        ];
        $Orderget = new Orderget();
        $RoleID = $Orderget->get_RoleID();
        $this->assign('RoleID', $RoleID);
        $this->assign('loanStatus', $loanStatus);

        return $this->fetch();
    }

    //	已完成页面
    //  当日到期页面
    public function yiwancheng_index()
    {
        $qudao = $this->qudao(); //调用渠道数据
        $this->assign('qudao', $qudao);

        return $this->fetch();
    }

    //	获取渠道信息
    public function qudao()
    {
        $qudao = Db::table('xb_tg_admin')->select();

        return $qudao;
    }

    //获取需要查询列表的条件
    //	$where 原来的搜索条件
    public function get_LoanStatus($where)
    {
        $songsong = input('songsong'); // 列表 区分字段
        $LoanStatus = input('LoanStatus'); //订单状态
        //		订单管理 ->待审核
        if ($songsong == 'daishenhe') {
            if (!$LoanStatus) {
                $where['LoanStatus'] = array('in', [7, 0]);
            }
        }
        //		订单管理 ->待放款
        if ($songsong == 'daifangkuan') {
            if (!$LoanStatus) {
                $where['LoanStatus'] = array('in', [1, 6]);
            }
        }
        //		订单管理 ->拒绝列表
        if ($songsong == 'jujue') {
            $where['LoanStatus'] = array('in', [5]);
        }
        //		还款计划->待还款->正常
        if ($songsong == 'huankuan_zhengchang') {
            if (!$LoanStatus) {
                if (empty($where['YyFkTime'])) {
                    $where['YyFkTime'] = array('>= time', date('Y-m-d'));
                }
                $where['LoanStatus'] = array('in', [2]);
            }
        }
        //		还款计划->待还款->逾期
        if ($songsong == 'huankuan_yuqi') {
            if (!$LoanStatus) {
                if (empty($where['YyFkTime'])) {
                    $where['YyFkTime'] = array('< time', date('Y-m-d'));
                }
                $where['LoanStatus'] = array('in', [2]);
            }
        }
        //		还款计划->当日到期
        if ($songsong == 'dangri') {
            if (empty($where['YyFkTime'])) {
                $where['YyFkTime'] = array('between time', [date('Y-m-d').' 00:00:00', date('Y-m-d').' 23:59:59']);
            }
            $where['LoanStatus'] = array('in', [2]);
            //			内催收人只能看到自己的订单
            $Orderget = new Orderget();
            $RoleID = $Orderget->get_RoleID();
            if ($RoleID == '51') {
                $where['cuishourenID'] = array('in', [session('id')]);
            }
        }
        //		还款计划->已完成
        if ($songsong == 'yiwancheng') {
            if (!$LoanStatus) {
                $where['LoanStatus'] = array('in', [3]);
            }
        }
        //	催收管理->逾期列表
        $where2 = []; //附加的搜索条件
           if ($songsong == 'yuqi') {
               if (!$LoanStatus) {
                   if (!empty($where['YyFkTime'])) {
                       $where2['YyFkTime'] = $where['YyFkTime'];
                   }
                   $where['YyFkTime'] = array('< time', date('Y-m-d')); //计算逾期
                   $where['LoanStatus'] = array('in', [2]);
               }
               //			外催收人只能看到自己的订单
               $Orderget = new Orderget();
               $RoleID = $Orderget->get_RoleID();
               if ($RoleID == '54') {
                   $where['cuishourenID'] = array('in', [session('id')]);
               }
           }
        $data['where'] = $where;
        $data['where2'] = $where2;

        return $data;
    }

    //	 订单数据  公用
    public function get_list()
    {
        $page = input('page'); //分页
        $limit = input('limit'); //显示个数
    	$songsong = input('songsong'); // 逾期用户按照 还款时间倒叙排序  暂时先这么写  以后可能会 调到上面统一处理 
           if ($songsong == 'yuqi') {
            $order='YyFkTime desc';
           }else{
           	$order='ApplyTime desc';
           }
        $Orderget = new Orderget();
        $where = $Orderget->sorch();
        $data = $this->get_LoanStatus($where);
        //		p($data);die;
        $where = $data['where']; //条件
        $where2 = $data['where2']; //YyFkTime有两个时间
        $re = Db::table('xb_loans_applylist a')->
        field('a.fail_reason,a.app_type,b.TgadminID,a.jishen,a.Interest,a.daoqiMoney,d.BankNo,a.FkadminID,a.pay,a.ShTime,a.user_type,a.ShadminID,a.cuishourenID,a.bufenhuankuan_num,a.bufenhuankuan,a.YyFkTime2,a.is_fujie,a.yuqililv,a.xjnums,a.SjFkTime,a.YyFkTime,a.OpenTime,a.ID,a.UserID,a.OrderSn,a.LoanStatus,b.TrueName,b.Mobile,b.IDCard,c.DaichaoName,a.ApplyMoney,a.ApplyDay,a.AdoptMoney,a.ApplyTime,b.MxScore')
        ->page($page, $limit)
        ->where($where)
        ->where($where2)
        ->join('xb_mem_info b', 'a.UserID=b.ID', 'LEFT')
        ->join('xb_tg_admin c', 'b.TgadminID=c.ID', 'LEFT')
        ->join('xb_renzen_bank d', 'a.UserID=d.UserID', 'LEFT')
        ->group('a.ID')
//		->fetchSql(true)
         ->order($order)->select();
//      ->order('ApplyTime desc')->select();
        //		p($re);die;
        $Orderget = new Orderget();
        $re = $Orderget->get_LoanStatus($re); //订单状态
        $re = $Orderget->get_pay_pay($re); //支付方式
        $re = $Orderget->get_app_type($re); //订单类型
        $re = $Orderget->get_cuishourenID($re); //催收人
        $re = $Orderget->get_FkadminID($re); //放款客服
        $re = $Orderget->get_ShadminID($re); //审核客服
        $re = $Orderget->get_user_type($re); //客户类型
        $re = $Orderget->get_xjnums($re); //是否展期
        $re = $Orderget->get_bufenhuankuan($re); //是否部分还款
        $re = $Orderget->get_beizhu($re); //最新的一条备注
        $re = $Orderget->get_Jktime($re); //借款次数
        foreach ($re as $k => $v) {
            $timelingche = strtotime(date('Y-m-d'), time()); //当天凌晨的时间戳
            $YyFkTime = strtotime($v['YyFkTime']); //预约还款日期转为时间戳
            $Orderget = new Orderget();
            $get_days_fakuan = $Orderget->get_days_fakuan($timelingche, $YyFkTime, $v);//未还款 订单中 逾期天数和罚款的算法
            $re[$k]['fakuan'] = $get_days_fakuan['fakuan']; //罚款
            $re[$k]['datas'] = $get_days_fakuan['datas']; //逾期天数
		    $SjFkTime=substr($v['SjFkTime'],0,10);
//			p($SjFkTime);die;
            $timesj = strtotime($SjFkTime, time()); //实际还款时间转为时间戳
            $get_days_fakuan_yiwancheng = $Orderget->get_days_fakuan($timesj, $YyFkTime, $v); //已完成订单中的  逾期天数 和罚款的算法
            $re[$k]['fakuan_yiwancheng'] = $get_days_fakuan_yiwancheng['fakuan']; //罚款
            $re[$k]['datas__yiwancheng'] = $get_days_fakuan_yiwancheng['datas']; //逾期天数
            if($re[$k]['datas__yiwancheng']>0){
              $re[$k]['huankuan_typr'] = '<span style="color:red">逾期还款</span>'; // 获取订单还款类型    正常还款/逾期还款
//             $re[$k]['yuqi_status_yiwancheng'] = '<span style="color:red">已经逾期</span>';
            }else{
            	$re[$k]['huankuan_typr'] = '<span style="color:green">正常还款</span>'; // 获取订单还款类型    正常还款/逾期还款
//          	$re[$k]['yuqi_status_yiwancheng'] = '<span style="color:green">未逾期</span>';
            }

            //			是否逾期
            if ($get_days_fakuan['datas'] > 0) {
                $re[$k]['yuqi_status'] = '<span style="color:red">已经逾期</span>';
            } else {
                $re[$k]['yuqi_status'] = '<span style="color:green">未逾期</span>';
            }
            $re[$k]['jianmian_money'] = $Orderget->get_jianmian_money($v['OrderSn']); // 应还减免
            $re[$k]['bufen_money'] = $Orderget->get_bufen_money($v['OrderSn']); //部分已还
            $re[$k]['dingdan_yinghuan_money'] = $Orderget->get_dingdan_yinghuan_money($v); //订单应还
            $re[$k]['muqian_yinghuan_money'] = $Orderget->get_muqian_yinghuan_money($v); // 目前应还
            $re[$k]['TgadminID'] = $Orderget->get_qudao($v['TgadminID']); // 渠道
            $re[$k]['shiji_money'] = $Orderget->get_shiji_money($v['ID']); // 获取订单实际还款金额
            
        }
        $re = $Orderget->get_SjFkTime_YyFkTime($re); //获取该订单的实际还款时间和预约还款时间   顺序不能换
      
        $cuont = Db::table('xb_loans_applylist a')
        ->where($where)
        ->where($where2)
        ->join('xb_mem_info b', 'a.UserID=b.ID', 'LEFT')
        ->join('xb_tg_admin c', 'b.TgadminID=c.ID', 'LEFT')
        ->join('xb_renzen_bank d', 'a.UserID=d.UserID', 'LEFT')
        ->count('a.ID');
        songsong($re, $cuont);
    }

//	个人订单数据
    public function get_list_dingdan()
    {
        $data = input();
        $page = input('page');
        $limit = input('limit');
        $ID = input('ID'); //用户ID
        $where = [];
        if ($ID) {
            $where['UserID'] = $ID;
        }
        $re = Db::table('xb_loans_applylist')
        ->page($page, $limit)->where($where)
        ->select();
        //订单状态 0申请中 1放款中 2已放款 3已完成 4已取消 5已拒绝 6等待银行打款 7待人工审核
        foreach ($re as $k => $v) {
            if ($v['LoanStatus'] == '0') {
                $re[$k]['LoanStatus'] = '申请中';
            }
            if ($v['LoanStatus'] == '1') {
                $re[$k]['LoanStatus'] = '<span style="color:red">放款中</span>';
            }
            if ($v['LoanStatus'] == '2') {
                $re[$k]['LoanStatus'] = '<span style="color:green">已放款</span>';
            }
            if ($v['LoanStatus'] == '3') {
                $re[$k]['LoanStatus'] = '<span style="color:black">已完成</span> ';
            }
            if ($v['LoanStatus'] == '4') {
                $re[$k]['LoanStatus'] = '<span>已取消</span>';
            }
            if ($v['LoanStatus'] == '5') {
                $re[$k]['LoanStatus'] = '<span style="color:#bdbd42">已拒绝</span>';
            }
            if ($v['LoanStatus'] == '6') {
                $re[$k]['LoanStatus'] = '等待银行打款';
            }
            if ($v['LoanStatus'] == '7') {
                $re[$k]['LoanStatus'] = '<span style="color:blue">待人工审核</span>';
            }
		  if ($v['LoanStatus'] == '3') {
//			$timelingche = strtotime(date('Y-m-d'), time()); //当天凌晨的时间戳
			$SjFkTime=substr($v['SjFkTime'],0,10);
            $YyFkTime = strtotime($v['YyFkTime']); //预约还款日期转为时间戳
            $timesj = strtotime($SjFkTime, time()); //实际还款时间转为时间戳
            $Orderget = new Orderget();
            $get_days_fakuan_yiwancheng = $Orderget->get_days_fakuan($timesj, $YyFkTime, $v); //已完成订单中的  逾期天数 和罚款的算法
            $re[$k]['fakuan'] = $get_days_fakuan_yiwancheng['fakuan']; //罚款
            $re[$k]['datas'] = $get_days_fakuan_yiwancheng['datas']; //逾期天数
            if($re[$k]['datas']>0){
              $re[$k]['huankuan_typr'] = '<span style="color:red">逾期还款</span>'; // 获取订单还款类型    正常还款/逾期还款
            }else{
            	$re[$k]['huankuan_typr'] = '<span style="color:green">正常还款</span>'; // 获取订单还款类型    正常还款/逾期还款
            }
			}
            //			是否展期期
            if ($v['xjnums'] >= 1) {
                $re[$k]['xjnums_status'] = '<span style="color:green">展期</span>';
            } else {
                $re[$k]['xjnums_status'] = '<span style="color:blue">未展期</span>';
            }
            //			是部分还款
            if ($v['bufenhuankuan'] == '1') {
                $re[$k]['bufenhuankuan'] = '<span style="color:green">是</span>';
            }
            if ($v['bufenhuankuan'] == '2') {
                $re[$k]['bufenhuankuan'] = '<span style="color:gray">否</span> ';
            }
            $ret = Db::table('xb_loans_applylist')->where('UserID', $v['UserID'])->select();
            $Jktime = count($ret);
            $re[$k]['Jktime'] = $Jktime;
        }
        $cuont = Db::table('xb_loans_applylist')
        ->where($where)
        ->count('ID');
        if ($re) {
            songsong($re, $cuont);
        } else {
            songsong($re, $cuont);
        }
    }
}
